package client.superadmin;

import static client.common.libs.Underscore._;
import static org.stjs.javascript.JSCollections.$array;
import static org.stjs.javascript.JSCollections.$map;

import org.stjs.javascript.Array;
import org.stjs.javascript.Map;

import client.common.EndPoints.SupadminEP;
import client.common.Models.Grado;
import client.common.Models.Materia;
import client.common.Models.MateriaGrado;
import client.common.angular.EmptyPromise;
import client.common.angular.NgScope;
import client.common.angular.Pickable;
import client.common.directives.EPChanging;
import client.common.directives.EPLoading;
import client.common.javascript.JsProcedure0;
import client.common.javascript.JsProcedure1;

public class ColGradoScope extends NgScope {

	private Grado grado;
	protected Array<Materia> materias;
	protected Array<Pickable<Materia>> materiasPickable;
	protected Array<Materia> materiasGrado;
	protected Map<String, Pickable<Materia>> materiasPickableMap;
	private EPChanging epChanging;
	private SupadminEP Superadmin;
	private EPLoading epLoading;
	private Map<String, Long> $stateParams;

	@SuppressWarnings("unchecked")
	public ColGradoScope(final Map<String, Long> $stateParams,
			final SupadminEP Superadmin, EPChanging epChanging,
			EPLoading epLoading) {
		super("ColGradoScope");
		this.materiasPickableMap = $map();
		this.materiasPickable = $array();
		this.epChanging = epChanging;
		this.Superadmin = Superadmin;
		this.epLoading = epLoading;
		this.$stateParams = $stateParams;

		final ColGradoScope that = this;
		this.grado = Superadmin.getGrado($stateParams.$get("idGrado"),
				new JsProcedure1<Grado>() {

					public void $invoke(Grado grado) {
						that.materias = Superadmin
								.getMateriasColegio($stateParams
										.$get("idColegio"));
						that.materiasGrado = Superadmin.getMateriasGrado(
								that.grado.id, $stateParams.$get("idColegio"));

						_.sync(new JsProcedure0() {
							public void $invoke() {
								for (int i = 0; i < that.materias.$length(); i++) {
									Materia materia = that.materias.$get(i);
									Pickable<Materia> pickable = new Pickable<Materia>();
									pickable.value = materia;
									pickable.selected = false;

									that.materiasPickable.push(pickable);
									that.materiasPickableMap.$put(materia.id
											+ "", pickable);
								}
								for (int i = 0; i < that.materiasGrado
										.$length(); i++) {
									Materia p = that.materiasGrado.$get(i);
									that.materiasPickableMap.$get(p.id + "").selected = true;
								}
							}
						}, that.materias, that.materiasGrado);
					}
				});

	}

	public void gradoChange() {
		final ColGradoScope that = this;
		epChanging.$invoke(this, 1300, new JsProcedure0() {
			public void $invoke() {
				that.epLoading.$invoke(
						that.Superadmin.updateGrado(that.grado.id, that.grado),
						"div[datos]");
			}
		});
	}

	public void onMateriaGradoChange(final Pickable<Materia> pickable,
			final boolean selected) {

		Materia materia = pickable.value;
		if (selected) {
			MateriaGrado materiaGrado = new MateriaGrado();
			materiaGrado.idMateria = materia.id;
			materiaGrado.idGrado = this.grado.id;
			materiaGrado.idColegio = $stateParams.$get("idColegio");

			Superadmin.newMateriaGrado(materiaGrado,
					new JsProcedure1<MateriaGrado>() {
						public void $invoke(MateriaGrado mg) {
							pickable.selected = selected;
						}

					});
		} else
			Superadmin.deleteMateriaGrado(materia.id, grado.id,
					$stateParams.$get("idColegio"),
					new JsProcedure1<EmptyPromise>() {
						public void $invoke(EmptyPromise p) {
							pickable.selected = selected;
						}
					});
	}

	public static Array<String> $inject() {
		return $array("$stateParams", "Superadmin", "epChanging", "epLoading");
	}

}
